const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Mock = require('mockjs')
const data = Mock.mock({
    'list|10': [{
        img: '@image(100x100,@color)',
        title: '@ctitle',
        phone: /^1[3-9]\d{9}$/,
        id: '@id'
    }]
})

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, './dist')
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
    ],
    mode: 'development',
    module: {
        rules: [{
            test: /\.scss$/,
            use: [{
                loader: "style-loader" // 将 JS 字符串生成为 style 节点
            }, {
                loader: "css-loader" // 将 CSS 转化成 CommonJS 模块
            }, {
                loader: "sass-loader" // 将 Sass 编译成 CSS
            }]
        }, {
            test: /\.(png|jpg|gif)$/,
            use: [{
                loader: 'file-loader',
                options: {}
            }]
        }, {
            test: /\.js$/,
            exclude: /(node_moudles|bower_components)/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['@babel/preset-env']
                }
            }
        }]
    },
    devServer: {
        host: 'localhost',
        port: 8080,
        hot: true,
        open: true,
        before(app) {
            app.get('/list', (req, res) => {
                res.send(data.list)
            })
            app.get('/detail', (req, res) => {
                res.send(data.list[0])
            })
        }
    }

}